package cz.proks.easyhelpdesk.gwt.restapi;

import java.util.Collection;

import org.jsonmaker.gwt.client.Jsonizer;
import org.spiffyui.client.rest.RESTCallback;
import org.spiffyui.client.rest.RESTility;

import com.google.gwt.core.client.GWT;
import com.google.gwt.json.client.JSONValue;

import cz.proks.easyhelpdesk.api.config.Configuration;
import cz.proks.easyhelpdesk.api.model.Thread;
import cz.proks.easyhelpdesk.api.model.ThreadJsonizer;
import cz.proks.easyhelpdesk.gwt.utils.JsonizerUtils;
import cz.proks.easyhelpdesk.gwt.utils.RestUtils;

public class ThreadRestApi extends BaseRestApi<Thread>{
    private final static Jsonizer JSONIZER = GWT.create(ThreadJsonizer.class);

    @Override
    protected String getBaseRestDomainUrlPart() {
        return Configuration.WS.THREAD_API_BASE_URL;
    }

    @Override
    protected Jsonizer getDomainJsonizer() {
        return JSONIZER;
    }    
    
    public void listForHelpdeskMemberWithPaging(Long helpdeskMemberId, Long from, Long to, final DomainListCallback<Thread> callback) {
        RestUtils.makeRestRequest(getBaseDomainUrl() + "/listForHelpdeskMember/" + helpdeskMemberId + "/withPaging/" + from + "-" + to, new RestUtils.Callback(callback) {
            
            @Override
            public void onSuccess(JSONValue val) {
                Collection<Thread> entities = JsonizerUtils.getListFromJsonArray(val, JSONIZER);
                callback.onDomainListSuccess(entities);
            }
        });
    }
    
    public void addTicket(Long id, Long ticketId, final RESTCallback callback) {
        RestUtils.makeRestRequest(getBaseDomainUrl() + "/" + id + "/addTicket/" + ticketId, null, RESTility.PUT, callback);
    }
    
    public void removeTicket(Long id, Long ticketId, final RESTCallback callback) {
        RestUtils.makeRestRequest(getBaseDomainUrl() + "/" + id + "/removeTicket/" + ticketId, null, RESTility.DELETE, callback);
    }
}
